import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
// import 'element-ui/lib/theme-default/index.css'
import 'element-ui/lib/theme-chalk/index.css';
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
// import Mock from './mock'
import axios from 'axios';
// Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'
import VueI18n from 'vue-i18n'

Vue.use(VueI18n) // 通过插件的形式挂载
Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

Vue.prototype.$http = axios;

//公共前缀,只要是axios请求，在请求上都加/api
// axios.defaults.baseURL = "/api";
axios.defaults.baseURL = "http://api.duanwendi.xyz";
Vue.prototype.$fastBaseUrl="http://121.37.194.36"
const router = new VueRouter({
    //去掉#号
    mode: 'history',
    routes
})

//路由拦截器
/*router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login') {
    sessionStorage.removeItem('user');
  }
  let user = JSON.parse(sessionStorage.getItem('user'));
  if (!user && to.path != '/login') {
    next({ path: '/login' })
  } else {
    next()
  }
})*/
// 路由拦截器
router.beforeEach((to, from, next) => {
    //如果你访问的界面是login或者商家入驻统一都应该放行
    if(to.path=="/login" || to.path=="/shopReg"){
        next();
    }else{
        //获取token的值
        var token = localStorage.getItem("token");
        //如果token有值，证明是登录状态
        if(token){
            //就应该是放行状态
            next();
        }else{
            //否则就应该跳转到登录界面
            next({path: '/login'});
        }
    }
})

//配置axios前置拦截器
/*axios前置拦截器， 只要你发送axios之前，都先执行次函数*/
axios.interceptors.request.use(config=>{
    //携带token
    let token =  localStorage.getItem("token");
    if(token){
        //添加请求头信息
        config.headers['token']=token;
    }
    return config;
},error => {
    Promise.reject(error);
})
//aixos后置拦截器，在响应目标函数之前，先执行此箭头函数,失败根据响应的状态判断
axios.interceptors.response.use(result=>{
    let data = result.data;
    if(!data.success&&data.message==="noLogin"){
        //把token和登录用户删除掉
        localStorage.removeItem("token");
        //把登录用户删除
        localStorage.removeItem("user");
        //跳转到登录界面
        router.push("/login");
        return;
    }
    if (data.message==="nopermission"){
        router.push("/nopermission");
        return ;
    }
    return result;
},error => {
    Promise.reject(error);
})

//router.afterEach(transition => {
//NProgress.done();
//});

//国际化
const i18n = new VueI18n({
    locale: 'zh-CN',    // 语言标识
    //this.$i18n.locale // 通过切换locale的值来实现语言切换
    messages: {
        'zh-CN': require('./common/lang/zh'),   // 中文语言包
        'en-US': require('./common/lang/en')    // 英文语言包
    }
})

new Vue({
    //el: '#app',
    //template: '<App/>',
    i18n,
    router,
    store,
    //components: { App }
    render: h => h(App)    //template: '<App/>',components: { App }  使用app组件
}).$mount('#app')   //跟el: '#app'  一样挂载

